// pages/speed/speed.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    speed1: '',
    speed2: '',
    speed3: ''
  },

  //第一个输入框值改变事件
  changeValue1: function(e) {
    let val = e.detail.value
    var num = val.match(/\./ig)
    if (num === null || num.length === 1) {
      this.setData({
        speed1: e.detail.value
      })
    } else {
      this.setData({
        speed1: this.data.speed1
      })
    }
  },

  //第二个输入框值改变事件
  changeValue2: function(e) {
    let val = e.detail.value
    var num = val.match(/\./ig)
    if (num === null || num.length === 1) {
      this.setData({
        speed2: e.detail.value
      })
    } else {
      this.setData({
        speed2: this.data.speed2
      })
    }
  },

  //第三个输入框值改变事件
  changeValue3: function(e) {
    let val = e.detail.value
    var num = val.match(/\./ig)
    if (num === null || num.length === 1) {
      this.setData({
        speed3: e.detail.value
      })
    } else {
      this.setData({
        speed3: this.data.speed3
      })
    }
  },

  //转换按钮点击事件
  transform: function(event) {
    console.log(event.currentTarget.dataset)
    let label = event.currentTarget.dataset.label;
    if (label == 'km/s') {
      let num = parseFloat(this.data.speed1);
      let val2 = num * 3600;
      let val3 = num * 1000
      this.setData({
        speed2: val2,
        speed3: val3
      })
    } else if (label == 'km/h') {
      let num = parseFloat(this.data.speed2);
      let val1 = num / 3600;
      let val3 = num * 1000 / 3600;
      this.setData({
        speed1: val1,
        speed3: val3
      })
    } else if (label == 'm/s') {
      let num = parseFloat(this.data.speed3);
      let val1 = num / 1000;
      let val2 = num / 1000 * 3600;
      this.setData({
        speed1: val1,
        speed2: val2
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})